class Solution {
public:
    int minPathSum(vector<vector<int>> &grid)
    {
        int m = grid.size();
        if(!m) {
            return 0;
        }
        int n = grid[0].size();
        for(int i = 0; i < m; ++i) {
            for(int j = 0; j < n; ++j) {
                if(!i && j) {
                    grid[i][j] = grid[i][j - 1] + grid[i][j];
                }
                if(!j && i) {
                    grid[i][j] = grid[i - 1][j] + grid[i][j];
                }
                if(i && j) {
                    grid[i][j] = min(grid[i][j - 1], grid[i - 1][j]) + grid[i][j];
                }
            }
        }
        return grid[m - 1][n - 1];
    }
};